Maps : Différences d’espérance de vie

Visu 2

Row

GDP

Depenses en santé

Row

Anemie

Eau potable

Visu 3

Row

Syrie.

Grenada.

Row

Venezuela.

---
title: "Espérance de vie"
author: "Amélie Rivet & Sofia Chemolli"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    social: menu
    source_code: embed
---

```{r setup, include=FALSE}
library(ggplot2)
library(tidyverse)
library(dplyr)
library(plotly)
library(flexdashboard)
```

Maps : Différences d'espérance de vie
=======================================================================

```{r data, eval = TRUE, echo = FALSE, results='hide', message = FALSE, warning=FALSE}
# Espérance de vie a la naissance
dta_off <- read.csv("Dta_off.csv", sep=",", stringsAsFactors = T)
dta_off <- dta_off[,3:23]
colnames(dta_off) <- c("Country","ISO3","2000", "2001", "2002","2003", "2004", "2005","2006", "2007", "2008","2009","2010", "2011", "2012","2013", "2014", "2015","2016","2017","2018")

dta_off <- dta_off %>% 
  pivot_longer(c("2000", "2001", "2002","2003", "2004", "2005","2006", "2007", "2008","2009","2010", "2011", "2012","2013", "2014", "2015","2016","2017","2018"))
colnames(dta_off)[3:4] <- c("Year", "Life.expectancy")
dta_off$Year <- as.factor(dta_off$Year)

dta_off <- dta_off[-c(4558:nrow(dta_off)),]


# GDP par habitant
dta_GDP <- read.csv("GDPDataBank.csv", sep=",", stringsAsFactors = T)
dta_GDP <- dta_GDP[,4:23]
colnames(dta_GDP) <- c("ISO3","2000", "2001", "2002","2003", "2004", "2005","2006", "2007", "2008","2009","2010", "2011", "2012","2013", "2014", "2015","2016","2017","2018")

dta_GDP <- dta_GDP %>% 
  pivot_longer(c("2000", "2001", "2002","2003", "2004", "2005","2006", "2007", "2008","2009","2010", "2011", "2012","2013", "2014", "2015","2016","2017","2018"))
colnames(dta_GDP)[2:3] <- c("Year", "GDP")
dta_GDP$Year <- as.factor(dta_GDP$Year)


# Dépenses sante % GDP
dta_dep <- read.csv("HealthExpDataBank.csv", sep=",", stringsAsFactors = T)
dta_dep <- dta_dep[,4:23]
colnames(dta_dep) <- c("ISO3","2000", "2001", "2002","2003", "2004", "2005","2006", "2007", "2008","2009","2010", "2011", "2012","2013", "2014", "2015","2016","2017","2018")

dta_dep <- dta_dep %>% 
  pivot_longer(c("2000", "2001", "2002","2003", "2004", "2005","2006", "2007", "2008","2009","2010", "2011", "2012","2013", "2014", "2015","2016","2017","2018"))
colnames(dta_dep)[2:3] <- c("Year", "Depense")
dta_dep$Year <- as.factor(dta_dep$Year)


# Anemie chez les enfants
dta_anemia <- read.csv("WHO_anemia.csv", sep=';', dec=',', stringsAsFactors = T)
dta_anemia$Year <- as.factor(dta_anemia$Year)


# Acces a l'eau potable
dta_water <- read.csv("WaterDataBank.csv", sep=",", stringsAsFactors = T)
dta_water <- dta_water[,4:23]
colnames(dta_water) <- c("ISO3","2000", "2001", "2002","2003", "2004", "2005","2006", "2007", "2008","2009","2010", "2011", "2012","2013", "2014", "2015", "2016","2017","2018")

dta_water <- dta_water %>% 
  pivot_longer(c("2000", "2001", "2002","2003", "2004", "2005","2006", "2007", "2008","2009","2010", "2011", "2012","2013", "2014", "2015", "2016","2017","2018"))
colnames(dta_water)[2:3] <- c("Year", "Water")
dta_water$Year <- as.factor(dta_water$Year)

dta <- left_join(dta_off, dta_dep, by = c("ISO3","Year"))

dta <- left_join(dta, dta_GDP, by = c("ISO3","Year"))

dta <- left_join(dta, dta_anemia[,-2], by = c("ISO3","Year"))

dta <- left_join(dta, dta_water, by = c("ISO3","Year"))

# Amélie
dta.diff <- dta %>% 
  group_by(ISO3) %>%
  summarise(Life.expectancy.diff = Life.expectancy[Year=='2018'] - Life.expectancy[Year=='2000'], 
            Depense.diff = Depense[Year=='2018'] - Depense[Year=='2000'], 
            GDP.diff = GDP[Year=='2018'] - GDP[Year=='2000'],
            Anemie.diff = Anemie[Year=='2018'] - Anemie[Year=='2000'], 
            Water.diff = Water[Year=='2018'] - Water[Year=='2000']) %>%
  arrange(Life.expectancy.diff)

# Sofia
diff.ISO3 <- dta_off %>% 
  filter(Year==2000 | Year==2018) %>% 
  group_by(ISO3) %>%
  summarise(Life.expectancy.diff = Life.expectancy[Year=='2018'] - Life.expectancy[Year=='2000'],
            Country = Country,
            Life.expectancy.2000 = Life.expectancy[Year=='2000'],
            Life.expectancy.2018 = Life.expectancy[Year=='2018']) %>% 
  arrange(Life.expectancy.diff) 
```

```{r vis1,  echo = FALSE,  message = FALSE, warning=FALSE}
fig <- plot_ly(diff.ISO3, 
               
               type = 'choropleth', 
               
               locations = diff.ISO3$ISO3, 
               
               z = diff.ISO3$Life.expectancy.diff, 
               
               text = paste("Pays : ", diff.ISO3$Country, '<br>', 
                            "Esperance de vie en 2000 : ", round(diff.ISO3$Life.expectancy.2000,1), '<br>', 
                            "Esperance de vie en 2018 : ", round(diff.ISO3$Life.expectancy.2018,1), '<br>', 
                            "Difference : ", round(diff.ISO3$Life.expectancy.diff,1)), 
               
               colorscale = "YlGnBu",
               
               hoverinfo = 'text')

m <- list(l = 100, r = 100, b = 100, t = 200, pad = 4)

fig %>% 
  
  colorbar(title = list(
    text = paste0("Différence de \nl'espérance de vie \nà la naissance \nentre 2018 et 2000 \n(en années)"), 
    font = list(size=16)), 
    x = 1, y = 0.7, len = 0.6) %>% 
  
  layout(title = list(text = paste0("Différence de l'espérance de vie moyenne par pays" , '<br>', '<sup>',  "entre 2018 et 2000"), 
                      font=list(size=20), xanchor = "middle", y=1.8, autosize = F, 
                      width = 400, height = 400, margin = m))
```

Visu 2
=======================================================================

Row
-----------------------------------------------------------------------


```{r, eval = TRUE, echo = FALSE, results='hide', message = FALSE, warning=FALSE, fig.keep='none'}
expectancy <- inner_join(dta, dta.diff, by='ISO3')

expectancy.2000 <- expectancy %>% 
  filter(Year==2000)

expectancy.2000[,4:13]<-round(expectancy.2000[,4:13],1)

summary(expectancy.2000)
```

### GDP

```{r, echo = FALSE,  message = FALSE, warning=FALSE}
g1 <- ggplot(data = expectancy.2000, 
                      aes(x = GDP.diff, y = Life.expectancy.diff, 
                          text=paste("Pays : ",Country,
                                     "<br>Espérance de vie en 2000 : ", Life.expectancy, 
                                     "<br>GDP en 2000 : ", GDP)), 
                      na.rm=TRUE) +
                 
                 geom_point(aes(color=Life.expectancy, size=GDP)) +
                 
                 scale_color_gradient(low ="#FFFF99",high = "#009933") +
                 
                 scale_y_continuous(breaks=seq(-10, 20, 5)) +
                 
                 scale_x_continuous(breaks=seq(-1000, 101000, 25000)) +
                 
                 labs(x = "Différence de GDP par habitant \nentre 2000 et 2018",
                      y = "Différence d'espérance de vie \nentre 2000 et 2018",
                      colour = "Espérance de vie \n en 2000",
                      size = "GDP par habitant \n en 2000") +
                 
                 ggtitle("Représentation de l'espérance de vie en fonction du GDP par habitant") +
                 
                 theme(plot.title = element_text(face="bold", hjust=0.5, size=10), 
                       axis.title.x = element_text(size=9),
                       axis.title.y = element_text(size=9),
                       panel.background = element_rect(fill = "white"),
                       panel.grid.major = element_line(colour = "#CCCCCC"),
                       plot.margin = unit(c(1,1,1,1), "cm"))

g1
```


### Depenses en santé

```{r, echo = FALSE,  message = FALSE, warning=FALSE}

g2 <- ggplot(data = expectancy.2000,
                      aes(x = Depense.diff, y = Life.expectancy.diff,
                          text = paste("Pays : ", Country,
                                       "<br>Espérance de vie en 2000 : ", Life.expectancy,
                                       "<br>Dépenses en santé en % GDP en 2000 : ", Depense," %")),
                      na.rm=TRUE) +

                 geom_point(aes(color=Life.expectancy, size=Depense)) +

                 scale_color_gradient(low ="#FFFF99",high = "#009933") +

                 scale_y_continuous(breaks=seq(-10, 20, 5)) +

                 scale_x_continuous(limits=c(-5,7.5), breaks=seq(-5, 7.5, 2.5)) +

                 labs(x = "Différence du pourcentage de dépenses \nen santé en pourcentage du GDP \nentre 2000 et 2018",
                      y = "Différence d'espérance de vie \nentre 2000 et 2018",
                      colour = "Espérance de vie \n en 2000",
                      size = "Dépenses en santé \n par habitant en 2000") +

                 ggtitle("Représentation de la relation espérance de vie - dépense en santé") +

                 theme(plot.title = element_text(face="bold", hjust=0.5, size=10),
                       axis.title.x = element_text(size=9),
                       axis.title.y = element_text(size=9),
                       panel.background = element_rect(fill = "white"),
                       panel.grid.major = element_line(colour = "#CCCCCC"),
                       plot.margin = unit(c(1,1,1,1), "cm"))

g2

```


Row
-----------------------------------------------------------------------

### Anemie

```{r, echo = FALSE,  message = FALSE, warning=FALSE}
g3 <- ggplot(data = expectancy.2000,
                      aes(x = Anemie.diff, y = Life.expectancy.diff,
                          text=paste("Pays : ",Country,
                                     "<br>Espérance de vie en 2000 : ", Life.expectancy,
                                     "<br> Enfants de moins de 5 ans \n atteint d'anémie en 2000 : ", Anemie, "%")),
                      na.rm=TRUE) +

                 geom_point(aes(color=Life.expectancy, size=Anemie)) +

                 scale_color_gradient(low ="#FFFF99",high = "#009933") +

                 scale_y_continuous(breaks=seq(-10, 20, 5)) +

                 scale_x_continuous(limits=c(-30, 5), breaks=seq(-30,5,5))+

                 labs(x = "Différence de pourcentage d'enfants de moins de \n5 ans atteints d'anémie \nentre 2000 et 2018",
                      y = "Différence d'espérance de vie \nentre 2000 et 2018",
                      colour = "Espérance de vie \n en 2000",
                      size = "% d'enfant atteints \n d'anémie en 2000") +

                 ggtitle("Représentation de la relation espérance de vie - Anémie") +

                 theme(plot.title = element_text(face="bold", hjust=0.5, size=10),
                       axis.title.x = element_text(size=9),
                       axis.title.y = element_text(size=9),
                       panel.background = element_rect(fill = "white"),
                       panel.grid.major = element_line(colour = "#CCCCCC"),
                       plot.margin = unit(c(1,1,1,1), "cm"))

g3
```

### Eau potable

```{r, echo = FALSE,  message = FALSE, warning=FALSE}
g4 <- ggplot(data = expectancy.2000,
                      aes(x = Water.diff, y = Life.expectancy.diff,
                          text=paste("Pays : ",Country,
                                     "<br>Espérance de vie en 2000 : ", Life.expectancy,
                                     "<br> Accès à l'eau potable en 2000 : ", Water, "%")),
                      na.rm=TRUE) +

                 geom_point(aes(color=Life.expectancy, size=Water)) +

                 scale_color_gradient(low ="#FFFF99",high = "#009933") +

                 scale_y_continuous(breaks=seq(-10, 20, 5)) +

                 scale_x_continuous(limits=c(-10,35), breaks=seq(-10, 30, 10)) +

                 labs(x = "Différence du pourcentage de personnes \nayant accès à l'eau potable \nentre 2000 et 2018",
                      y = "Différence d'espérance de vie \nentre 2000 et 2018",
                      colour = "Espérance de vie \n en 2000",
                      size = "% de personnes \n ayant accès à l'eau potable en 2000") +

                 ggtitle("Représentation de la rélation entre les différences d'espérance de vie \n et de plusieurs indicateurs entre 2000 et 2018") +

                 theme(plot.title = element_text(face="bold", hjust=0.5, size=10),
                       axis.title.x = element_text(size=9),
                       axis.title.y = element_text(size=9),
                       panel.background = element_rect(fill = "white"),
                       panel.grid.major = element_line(colour = "#CCCCCC"),
                       plot.margin = unit(c(1,1,1,1), "cm"))
g4
```


Visu 3
=======================================================================

Row
-----------------------------------------------------------------------

### Syrie.

```{r}
dta_SYR <- subset(dta, ISO3 == "SYR")
old.y <- list(
  side = "left",
  range=c(69,75)
)
new.y <- list(
  overlaying = "y",
  side = "right",
  title ="Dépenses en santé (en % du GDP)",
  range=c(3,5.5),
  showgrid=F
)
fig1 <- plot_ly(dta_SYR) %>%
  
  add_lines(x = ~Year, y = ~Life.expectancy, yaxis="y1", name="Espérance de vie", 
            line = list(color = 'black', width = 2)) %>%
  
  add_lines(x = ~Year, y = ~Depense, yaxis = "y2", name="Dépenses en santé",
            line = list(color = '#00AFBB', width = 2)) %>%
  
  layout(yaxis2 = new.y,
         
         yaxis = list(old.y, showgrid = T, title="Espérance de vie moyenne à la naissance \n (en années)"), 
         
         xaxis = list(title="Années", showgrid = F),
         
         legend = list(title=list(text=''), y = 73,
                       textfont = list(family = "Arial", size = 14, color = toRGB("black"))),
         
         title = list(text = paste0("Evolution de l'espérance de vie de la Syrie", '<br>',
                                    '<sup>', "Entre 2000 et 2018"),
                      xanchor = 'left', x=0.1),
         
         shapes = list(
               list(type = "rect",
                    fillcolor = "lightblue", line = list(color = "lightblue"), opacity = 0.3,
                    x0 = "11", x1 = "18",
                    y0 = 68.5, y1 = 75))) %>%
  
  add_annotations(x = 14.5, y = 74.5, 
    text  = "Guerre civile depuis 2011", 
    xanchor = 'center',
    showarrow = F) %>%
  add_annotations(x = 11, y = 69, 
    text  = "Chute du GDP entre 2010 et 2012 : \n5,5 fois plus faible", 
    xanchor = 'center',
    showarrow = F)
fig1
```

### Grenada.

```{r, warning=F}
ggplot(data = dta_off[dta_off$ISO3 == "GRD",], 
       aes(x = Year, y = Life.expectancy, group=1)) + 
  
  geom_line(color = "#00AFBB", size = 2) +
  
  geom_point() +
  
  # Ouragan Ivan 2004
  annotate(geom = "curve", 
           x = 3.5, xend = 5,
           y = 73.15, yend = dta_off$Life.expectancy[dta_off$Country=="Grenada"&dta_off$Year=="2004"] + 0.03, 
           curvature = -0.2, 
           arrow = arrow(length = unit(2, "mm")),
            col ="#00AFBB") +
  
  annotate(geom = "text", 
           x = 3.4, y = 73.15, 
           label = "Ouragan Ivan", 
           hjust = "right",
           col = "#00AFBB") +
  
  geom_segment(aes(x=5, xend=5, 
                   y = min(dta_off$Life.expectancy[dta_off$Country=="Grenada"]), 
                   yend = dta_off$Life.expectancy[dta_off$Country=="Grenada"&dta_off$Year=="2004"]),
               linetype="longdash", col ="#00AFBB") + 
  
  # Ouragan Emily 2005
  annotate(geom = "curve", 
           x = 7.5, xend = 6,
           y = 73.2, yend = dta_off$Life.expectancy[dta_off$Country=="Grenada"&dta_off$Year=="2005"] + 0.03, 
           curvature = .2, 
           arrow = arrow(length = unit(2, "mm"))) +
  
  annotate(geom = "text", 
           x = 7.6, y = 73.2, 
           label = "Ouragan Emily", 
           hjust = "left") +
  
  geom_segment(aes(x=6, xend=6, 
                   y = min(dta_off$Life.expectancy[dta_off$Country=="Grenada"]), 
                   yend = dta_off$Life.expectancy[dta_off$Country=="Grenada"&dta_off$Year=="2005"]),
               linetype="longdash") +
  
  theme_classic() +
  
  labs(title="Evolution de l'espérance de vie de Grenada", # ajout du titre
       subtitle='Entre 2000 et 2018',
       x = 'Années',
       y = 'Espérance de vie moyenne à la naissance \n (en années)')  # ajout du sous titre
```


Row
-----------------------------------------------------------------------

### Venezuela.

```{r, warning=F}
GDP_VEN <- read.csv("GDP_VEN.csv", sep=";")
colnames(GDP_VEN) <- c("2000", "2001", "2002","2003", "2004", "2005","2006", "2007", "2008","2009","2010", "2011", "2012","2013", "2014", "2015","2016","2017","2018")
GDP_VEN <- GDP_VEN %>% 
  pivot_longer(c("2000", "2001", "2002","2003", "2004", "2005","2006", "2007", "2008","2009","2010", "2011", "2012","2013", "2014", "2015","2016","2017","2018"))
colnames(GDP_VEN)[1:2] <- c("Year", "GDP")
GDP_VEN$Year <- as.factor(GDP_VEN$Year)
GDP_VEN$GDP_obs <- c(GDP_VEN$GDP[1:15], rep(NA,4))
GDP_VEN$GDP_pred <- c(rep(NA,14),GDP_VEN$GDP[15:19])
dta_dual_y <- data.frame(dta_off[dta_off$ISO3 == "VEN",])
dta_dual_y$GDP_obs <- c(GDP_VEN$GDP[1:15], rep(NA,4))
dta_dual_y$GDP_pred <- c(rep(NA,14),GDP_VEN$GDP[15:19])
old.y <- list(
  side = "left"
)
new.y <- list(
  overlaying = "y",
  side = "right",
  title = "GDP par habitant (USD)"
)
fig2 <- plot_ly(dta_dual_y) %>%
  
  add_lines(x = ~Year, y = ~Life.expectancy, yaxis="y1", name="Esperance de vie", 
            line = list(color = 'black', width = 2)) %>%
  
  add_lines(x = ~Year, y = ~GDP_obs, yaxis = "y2", name="GDP (données officielles)",
            line = list(color = '#00AFBB', width = 2)) %>%
  
  add_lines(x = ~Year, y = ~GDP_pred, yaxis = "y2", name="GDP (données estimées)",
            line = list(color = '#00AFBB', width = 2, dash = 'dot')) %>%
  
  layout(yaxis2 = new.y,
         
         yaxis = list(old.y, showgrid = F, title="Espérance de vie moyenne à la naissance \n (en années)"), 
         
         xaxis = list(title="Années", showgrid = F),
         
         legend = list(title=list(text=''), y = 73,
                       textfont = list(family = "Arial", size = 14, color = toRGB("black"))),
         
         title = list(text = paste0("Evolution de l'espérance de vie du Venezuela", '<br>',
                                    '<sup>', "Entre 2000 et 2018"),
                      xanchor = 'left', x=0.1),
         
         shapes = list(
               list(type = "rect",
                    fillcolor = "lightblue", line = list(color = "lightblue"), opacity = 0.2,
                    x0 = "0", x1 = "13",
                    y0 = 72, y1 = 73.5),
               list(type = "rect",
                    fillcolor = "#009EFF", line = list(color = "#009EFF"), opacity = 0.2,
                    x0 = "13", x1 = "18",
                    y0 = 72, y1 = 73.5))) %>%
           
  add_text(showlegend = FALSE, 
           x = c("2004","2016"), y = c(73.4, 73.4),
           text = c("Présidence d'Hugo Chávez","Présidence de \nNicolás Maduro"),
           textfont = list(family = "Arial", size = 14, color = toRGB("black")))
fig2
```

###

```{r}

```